package user;

import book.Library;
import utils.PermissionException;
import constant.Constant;

public class ProxyUser {

    private  User realUser;

    private Library library = Library.getLibrary();

    public ProxyUser(User user) {
        realUser = user;
    }

    public User getRealUser() {
        return realUser;
    }

    /**
     * 管理员权限检查方法
     * @param msg
     */
    private void checkRealUserWhetherAdminUser(String msg) {
        if(!(realUser instanceof AdminUser)) {
            throw new PermissionException(msg);
        }
    }


    //其他操作
    /*//调用菜单
    public int display() {
        return -1;
    }*/
    //添加书籍操作
    public void addBook() {
        System.out.println("上架图书...");
        checkRealUserWhetherAdminUser("普通用户没有权限上架图书");
        ((AdminUser)realUser).addBook();
    }

    //更新书籍操作
    public void updateBook() {
        System.out.println("更新图书...");
        checkRealUserWhetherAdminUser("普通用户没有权限更新图书");
        ((AdminUser)realUser).updateBook();
    }

    //移除图书
    public void removeBook() {
        System.out.println("删除图书...");

        checkRealUserWhetherAdminUser("普通用户没有权限删除图书");
        ((AdminUser)realUser).removeBook();

    }

    //查看图书的借阅次数
    public void borrowCount( ) {
        System.out.println("查看图书的借阅次数...");

        checkRealUserWhetherAdminUser("普通用户没有权限查看图书的借阅次数");
        ((AdminUser)realUser).borrowCount();
    }

    //查看最受欢迎的前K本书
    public void generateBook() {
        System.out.println("查看最受欢迎的前K本书...");
        checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前K本书");
        ((AdminUser)realUser).generateBook();
    }

    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("查看库存状态...");

        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态...");
        ((AdminUser)realUser).checkInventoryStatus();
    }


    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        System.out.println("移除上架超过1年的书籍...");

        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态...");
        ((AdminUser)realUser).checkAndRemoveOldBooks();

    }
    //--------------------------------普通相关方法------------------------------//
    /**
     * 普通用户权限检查方法
     * @param msg
     */
    private void checkRealUserWhetherNormalUser(String msg) {
        if(!(realUser instanceof NormalUser)) {
            throw new PermissionException(msg);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("借阅图书...");

        checkRealUserWhetherNormalUser("请以普通用户的身份借阅书籍");

        ((NormalUser)realUser).borrowBook();

    }

    //归还图书
    public void returnBook() {
        System.out.println("归还图书...");

        checkRealUserWhetherNormalUser("请以普通用户的身份归还书籍");

        ((NormalUser)realUser).returnBook();
    }


    //查看个人借阅情况
    public void viewBorrowHistory() {
        System.out.println("查看个人借阅情况...");

        checkRealUserWhetherNormalUser("请以普通用户的身份归还书籍");

        ((NormalUser)realUser).viewBorrowHistory();
    }

    public void handleOperation(int choice) {
        if(realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    //打印图书
                    library.displyBook();
                    break;
                case Constant.EXIT:
                    //退出系统
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    break;
            }
        }else if(realUser instanceof NormalUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    //打印图书
                    library.displyBook();
                    break;
                case Constant.EXIT:
                    //退出系统
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
            }
        }

    }
}
